import 'dart:ui';
import 'package:flutter/animation.dart';
import 'package:flutter_svg/flutter_svg.dart' as svg;
import 'package:hixing_app/views/test_view/canvas_utils.dart';
import 'package:hixing_app/views/test_view/reset_test_matrix4/model/matrix4_data_model.dart';
import 'package:hixing_app/views/test_view/test_matrix4/rect_element_model.dart';
import 'package:vector_math/vector_math_64.dart';
import 'package:vector_math/vector_math_64.dart' as vector;
import 'dart:math' as math;

class ActionPicture {
  // 是否锁定比例
  bool isLock;

  // 旋转后的中心点
  Offset centerOffset;

  // 点击矩形列表
  List<RectInfoModel> rectList;

  // 定义矩阵
  Matrix4 matrix4;

  // 定义矩阵
  Matrix4 lastMatrix4;

  // 初始化矩阵数据

  Matrix4DataModel matrix4Data;

  // 图片size
  Size imgSize;

  //icon size
  Size iconSize;

  /// 上一次旋转的角度
  double lastAngle = 0;

  /// 第一次触摸的角度
  double initialAngle = 0;

  /// 旋转的angle
  double angle = 0.0;

  // 图片对象
  svg.PictureInfo? imgSvgPicture;

  ActionPicture({
    this.isLock = true,
    this.centerOffset = const Offset(0, 0),
    this.imgSize = const Size(50, 100),
    this.iconSize = const Size(30, 30),
    this.imgSvgPicture,
    this.lastAngle = 0.0,
    this.initialAngle = 0.0,
    this.angle = 0.0,
    required this.rectList,
    required this.matrix4,
    required this.lastMatrix4,
    required this.matrix4Data,
  });

  static ActionPicture initActionPicture({
    Matrix4DataModel? initMatrixData,
    bool isLock = true,
    Size initImgSize = const Size(50, 100),
    Size initIconSize = const Size(30, 30),
    required initImgSvgPicture,
  }) {
    Matrix4DataModel initData = initMatrixData ??
        Matrix4DataModel(
            dx: 100,
            dy: 100,
            scaleX: 1,
            scaleY: 1,
            rotationAngle: 0,
            skewX: 0.0,
            skewY: 0.0);
    Matrix4 initMatrix =
        CanvasUtils.dataToMatrix4(initData, imgSize: initImgSize);
    return ActionPicture(
        matrix4: initMatrix,
        imgSize: initImgSize,
        iconSize: initIconSize,
        rectList: [],
        imgSvgPicture: initImgSvgPicture,
        lastMatrix4: Matrix4.identity(),
        matrix4Data: initData);
  }

  ///计算两个点之间的角度
  double calculateAngle(Offset origin, Offset position) {
    final double dx = position.dx - origin.dx;
    final double dy = position.dy - origin.dy;
    return math.atan2(dy, dx);
  }

  /// 更新第一次触摸的旋转角度,需要传入第一次触摸的点
  void updateInitialAngle(Offset position) {
    initialAngle = calculateAngle(centerOffset, position);
  }

  /// 更新矩形上一次旋转的角度
  void updateLastAngle(double angle) {
    lastAngle = angle;
  }

  void updateAngle(double angle) {
    this.angle = angle;
  }

  /// 计算点旋转后的坐标，角度写反便是坐标旋转后的校正
  Offset transformOffset(Offset offset, double angle) {
    final double rad = angle;
    final double sinRad = math.sin(rad);
    final double cosRad = math.cos(rad);
    return Offset(
      offset.dx * cosRad - offset.dy * sinRad,
      offset.dx * sinRad + offset.dy * cosRad,
    );
  }

  /// 更新矩形中心
  void updateCenterOffset() {
    // 计算当前顶点的中心 旋转的时候，使用这个新的中心点旋转
    List<Offset> vertex =
        CanvasUtils.getVertex(imgSize: imgSize, matrix: matrix4);

    // 绘制图片的矩形操作框
    Path imgTapPath = Path()..moveTo(vertex[0].dx, vertex[0].dy); // 移动到第一个顶点
    for (int i = 1; i < vertex.length; i++) {
      imgTapPath.lineTo(vertex[i].dx, vertex[i].dy); // 添加线段
    }
    imgTapPath.close(); // 封闭路径
    vector.Vector3 currentPosition = lastMatrix4.getTranslation();
    // 把新的中心点计算进去
    centerOffset = Offset(currentPosition.x + imgTapPath.getBounds().center.dx,
        currentPosition.y + imgTapPath.getBounds().center.dy);
  }

  List<Offset> getCurrentVertex() {
    Matrix4DataModel data = CanvasUtils.matrix4ToData(matrix4);
    List<Offset> vertex =
        CanvasUtils.getVertex(imgSize: imgSize, matrix: matrix4);
    List<Offset> curVertex = [];
    for (var item in vertex) {
      // print("当前位移的坐标${_data.dx},${_data.dy}");
      curVertex.add(item + Offset(data.dx, data.dy));
    }
    return curVertex;
  }

  Matrix4DataModel getLastMatrixData() {
    Matrix4DataModel data = CanvasUtils.matrix4ToData(lastMatrix4);
    return data;
  }

  void saveLastMatrix() {
    lastMatrix4 = matrix4.clone();
  }

  void addTranslate(Offset position) {
    matrix4 = (Matrix4.identity()..translate(position.dx, position.dy)) *
        lastMatrix4.clone();
  }

  void addScale(double scaleX, double scaleY,
      {Offset scaleOffset = Offset.zero}) {
    matrix4 = lastMatrix4.clone()
      ..translate(scaleOffset.dx, scaleOffset.dy)
      ..scale(scaleX, scaleY);
  }

  void addRotation(double angle, {Offset? original}) {
    Offset oCenter = original ?? centerOffset;
    matrix4 = (Matrix4.identity()
          ..translate(oCenter.dx, oCenter.dy)
          ..rotateZ(angle)
          ..translate(-oCenter.dx, -oCenter.dy)) *
        lastMatrix4.clone();
  }
}
